{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "865b5e7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt \n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "19ca1bfa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "# создадим список, содержащий натуральные числа от 1 до 10 \n",
    "x = list(range(1, 11)) \n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0b1df0a",
   "metadata": {},
   "source": [
    "## Min-max нормализация"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cca85fa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 0.11, 0.22, 0.33, 0.44, 0.56, 0.67, 0.78, 0.89, 1.0]\n"
     ]
    }
   ],
   "source": [
    "# определяем минимальное и максимальное значение в оригинальной структуре данных \n",
    "min_x = min(x) \n",
    "max_x = max(x)  \n",
    "\n",
    "# рассчитываем нормализованные значения элементов оригинальной структуры данных \n",
    "x_normalized_min_max = [round((x_i - min_x) / (max_x - min_x), 2) for x_i in x] \n",
    "print(x_normalized_min_max)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b90a9a",
   "metadata": {},
   "source": [
    "## Нормализация по среднему"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "abfb9c1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.0, -0.78, -0.56, -0.33, -0.11, 0.11, 0.33, 0.56, 0.78, 1.0]\n"
     ]
    }
   ],
   "source": [
    "# вычисляем среднее значение в оригинальной структуре данных \n",
    "mean_x = sum(x) / len(x)  \n",
    "\n",
    "# рассчитываем нормализованные по среднему значения элементов оригинальной структуры данных \n",
    "x_normalized_mean = [round((x_i - mean_x) / (max_x - mean_x), 2) for x_i in x] \n",
    "print(x_normalized_mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcd7ffa6",
   "metadata": {},
   "source": [
    "## Стандартизация (Нормализация Z-оценки)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e62e3d6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.57, -1.22, -0.87, -0.52, -0.17, 0.17, 0.52, 0.87, 1.22, 1.57]\n"
     ]
    }
   ],
   "source": [
    "# вычисляем дисперсию в оригинальной структуре данных \n",
    "variance_x = sqrt(sum([((x_i - mean_x) ** 2) for x_i in x])  / len(x))  \n",
    "\n",
    "# рассчитываем стандартизированные значения элементов оригинальной структуры данных \n",
    "x_standardized = [round((x_i - mean_x) / variance_x, 2) for x_i in x] \n",
    "print(x_standardized)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d2517bd",
   "metadata": {},
   "source": [
    "## Масштабирование"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "81a27cfa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.05, 0.1, 0.15, 0.2, 0.25, 0.31, 0.36, 0.41, 0.46, 0.51]\n"
     ]
    }
   ],
   "source": [
    "# вычисляем L2-норму оригинального вектора \n",
    "l2_norm = sqrt(sum([x_i ** 2 for x_i in x]))  \n",
    "\n",
    "# масштабируем элементы списка \n",
    "x_scaled = [round(x_i / l2_norm, 2) for x_i in x] \n",
    "print(x_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5f23fb5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
